package com.zhilianbao.leyaogo.ui.adapter.home.applista;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bql.utils.CheckUtils;
import com.bql.utils.NumberUtlis;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.listener.OnAddToCartClickListener;
import com.zhilianbao.leyaogo.utils.ImageUtils;
import com.zhilianbao.leyaogo.utils.Utils;
import com.zhilianbao.leyaogo.view.widgets.MoneyTextView;

import me.drakeet.multitype.ItemViewProvider;

/**
 * 首页专题 内容 ItemViewProvider
 */
public class HomeTopicContentViewProvider
        extends ItemViewProvider<HomeTopicContent, HomeTopicContentViewProvider.ViewHolder> {

    private OnAddToCartClickListener mOnAddToCartClickListener;

    public HomeTopicContentViewProvider(OnAddToCartClickListener onAddToCartClickListener) {
        mOnAddToCartClickListener = onAddToCartClickListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_home_topic_content, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(
            @NonNull ViewHolder holder, @NonNull HomeTopicContent homeTopicContent) {
        ImageUtils.loadHomeGoodsPic(Utils.returnUrlShow(homeTopicContent.getGoodsPic()), holder.mGoodPic);
        holder.mGoodName.setText(homeTopicContent.getGoodsTitle());
        holder.mAddToCart.setOnClickListener(v -> {
            if (mOnAddToCartClickListener != null) {
                mOnAddToCartClickListener.onAddToCartClick(homeTopicContent, holder.mGoodPic);
            }
        });
        holder.mLlInfo.setOnClickListener(v ->
                Utils.startGoodsDetails((Activity) holder.mLlInfo.getContext(), homeTopicContent.getGoodsId(), homeTopicContent.getGoodSkuId(), false)
        );

        switch (homeTopicContent.getType()) {
            //单品
            case 0:
                holder.mAccountPrice.setAmount(homeTopicContent.getAccountPrice());
                holder.mOriginPrice.setAmount(homeTopicContent.getOriginPrice());
                holder.mGoodDes.setText(homeTopicContent.getGoodsDes());
                break;

            //分组
            case 1:
                if (homeTopicContent.getGoodsType() == 1 && homeTopicContent.getGoodSkuId() != 0) {
                    holder.mTvSkuName.setText(homeTopicContent.getGoodSkuName());
                    holder.mAccountPrice.setAmount(homeTopicContent.getSkuPrice());
                    holder.mOriginPrice.setAmount(homeTopicContent.getSkuMPrice());
                    holder.mGoodDes.setText(homeTopicContent.getGoodSkuName());
                } else {
                    holder.mTvSkuName.setText("");
                    holder.mGoodDes.setText(homeTopicContent.getGoodsDes());
                    holder.mAccountPrice.setAmount(homeTopicContent.getAccountPrice());
                    holder.mOriginPrice.setAmount(homeTopicContent.getOriginPrice());
                }
                break;

            //活动专题
            case 2:
            case 3:
                if (homeTopicContent.getGoodsType() == 1 && homeTopicContent.getGoodSkuId() != 0) {
                    holder.mTvSkuName.setText(homeTopicContent.getGoodSkuName());
                    holder.mGoodDes.setText(homeTopicContent.getGoodSkuName());
                    //                    holder.mAccountPrice.setAmount(homeTopicContent.getSkuPrice());
                    //                    holder.mOriginPrice.setAmount(homeTopicContent.getSkuMPrice());
                } else {
                    holder.mTvSkuName.setText("");
                    holder.mAccountPrice.setAmount(homeTopicContent.getGoodsPrice());
                    //                    holder.mOriginPrice.setAmount(homeTopicContent.getOriginPrice());
                    holder.mGoodDes.setText(homeTopicContent.getGoodsDes());
                }

                //折扣商品
                if (homeTopicContent.getPriceType() == 1) {
                    if (homeTopicContent.getGoodsType() == 1 && homeTopicContent.getGoodSkuId() != 0) {
                        holder.mAccountPrice.setAmount(Utils.calculateAccountPrice(NumberUtlis.convertTodouble(homeTopicContent.getSkuPrice(), 0), NumberUtlis.convertTodouble(homeTopicContent.getAccountPrice(), 0)));
                    } else {
                        holder.mAccountPrice.setAmount(Utils.calculateAccountPrice(NumberUtlis.convertTodouble(homeTopicContent.getGoodsPrice(), 0), NumberUtlis.convertTodouble(homeTopicContent.getAccountPrice(), 0)));
                    }
                } else {
                    holder.mAccountPrice.setAmount(homeTopicContent.getAccountPrice());
                }
                break;
        }
        if (!CheckUtils.isEmpty(homeTopicContent.getActivitySign())) {
            holder.mTvLabel.setVisibility(View.VISIBLE);
            holder.mTvLabel.setText(homeTopicContent.getActivitySign());
        } else {
            holder.mTvLabel.setVisibility(View.GONE);
        }


    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView mGoodPic;
        private TextView mGoodName;
        private TextView mGoodDes;
        private MoneyTextView mAccountPrice;
        private MoneyTextView mOriginPrice;
        private ImageView mAddToCart;
        private LinearLayout mLlInfo;
        private TextView mTvSkuName;
        private TextView mTvLabel;

        ViewHolder(View itemView) {
            super(itemView);
            mGoodPic = (ImageView) itemView.findViewById(R.id.iv_topic_pic);
            mGoodName = (TextView) itemView.findViewById(R.id.tv_topic_name);
            mGoodDes = (TextView) itemView.findViewById(R.id.tv_topic_small);
            mAccountPrice = (MoneyTextView) itemView.findViewById(R.id.tv_account_price);
            mOriginPrice = (MoneyTextView) itemView.findViewById(R.id.tv_origin_price);
            mAddToCart = (ImageView) itemView.findViewById(R.id.iv_add);
            mLlInfo = (LinearLayout) itemView.findViewById(R.id.ll_info);
            mTvSkuName = (TextView) itemView.findViewById(R.id.tv_goods_sku);
            mTvLabel = (TextView) itemView.findViewById(R.id.rv_label);
        }
    }
}